/*     */ package net.sourceforge.appgen.xml;
/*     */ 
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.File;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.xml.parsers.DocumentBuilder;
/*     */ import javax.xml.parsers.DocumentBuilderFactory;
/*     */ import javax.xml.parsers.ParserConfigurationException;
/*     */ import javax.xml.transform.Transformer;
/*     */ import javax.xml.transform.TransformerConfigurationException;
/*     */ import javax.xml.transform.TransformerException;
/*     */ import javax.xml.transform.TransformerFactory;
/*     */ import javax.xml.transform.dom.DOMSource;
/*     */ import javax.xml.transform.stream.StreamResult;
/*     */ import net.sourceforge.appgen.model.ConnectionInformation;
/*     */ import net.sourceforge.appgen.model.Entity;
/*     */ import net.sourceforge.appgen.model.Field;
/*     */ import net.sourceforge.appgen.model.GenerationInformation;
/*     */ import net.sourceforge.appgen.model.MappingData;
/*     */ import org.w3c.dom.DOMImplementation;
/*     */ import org.w3c.dom.Document;
/*     */ import org.w3c.dom.Element;
/*     */ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/*     */ 
/*     */ public class XmlData
/*     */ {
/*     */   public static final String ROOT_ELEMENT_TAG_NAME = "appgen-mapping";
/*     */   public static final String CONNECTION_INFORMATION_TAG_NAME = "connectionInformation";
/*     */   public static final String DATABASE_NAME_TAG_NAME = "databaseName";
/*     */   public static final String DATABASE_SCHEMA_TAG_NAME = "databaseSchema";
/*     */   public static final String GENERATION_INFORMATION_TAG_NAME = "generationInformation";
/*     */   public static final String OUTPUT_DIR_TAG_NAME = "outputDir";
/*     */   public static final String PACKAGE_NAME_TAG_NAME = "packageName";
/*     */   public static final String ENTITY_LIST_TAG_NAME = "entityList";
/*     */   public static final String ENTITY_TAG_NAME = "entity";
/*     */   public static final String TABLE_NAME_ATTR_NAME = "tableName";
/*     */   public static final String BASE_NAME_ATTR_NAME = "baseName";
/*     */   public static final String CREATE_ATTR_NAME = "create";
/*     */   public static final String FIELD_LIST_TAG_NAME = "fieldList";
/*     */   public static final String FIELD_TAG_NAME = "field";
/*     */   public static final String COLUMN_NAME_ATTR_NAME = "columnName";
/*     */   public static final String COLUMN_TYPE_ATTR_NAME = "columnType";
/*     */   public static final String COLUMN_LENGTH_ATTR_NAME = "columnLength";
/*     */   public static final String PK_POSITION_ATTR_NAME = "pkPosition";
/*     */   public static final String LOB_ATTR_NAME = "lob";
/*     */   public static final String NULLABLE_ATTR_NAME = "nullable";
/*     */   public static final String FIELD_NAME_ATTR_NAME = "fieldName";
/*     */   public static final String FIELD_TYPE_ATTR_NAME = "fieldType";
/*     */   public static final String XSI_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema-instance";
/*     */   public static final String NAMESPACE_URI = "http://appgen.sourceforge.net/appgen-mapping";
/*     */   public static final String XSI_SCHEMA_LOCATION = "xsi:schemaLocation";
/*     */   public static final String SCHEMA_LOCATION = "http://appgen.sourceforge.net/schema/appgen-mapping-1.0.0.xsd";
/*     */   private MappingData mappingData;
/*     */ 
/*     */   public XmlData()
/*     */   {
/*  96 */     this(null);
/*     */   }
/*     */ 
/*     */   public XmlData(MappingData mappingData) {
/* 100 */     this.mappingData = mappingData;
/*     */   }
/*     */ 
/*     */   public void saveToXml(File file) throws XmlDataException {
/*     */     try {
/* 105 */       Document document = createDocument();
/*     */ 
/* 107 */       createXmlFile(file, document);
/*     */     } catch (Exception e) {
/* 109 */       throw new XmlDataException(e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getXmlText() throws XmlDataException {
/*     */     try {
/* 115 */       Document document = createDocument();
/*     */ 
/* 117 */       return getXmlText(document); 
				} catch (Exception e) {
					throw new XmlDataException(e);
/*     */     }
/* 119 */     
/*     */   }
/*     */ 
/*     */   private Document createDocument() throws ParserConfigurationException
/*     */   {
/* 124 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 125 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 126 */     DOMImplementation dom = builder.getDOMImplementation();
/*     */ 
/* 128 */     Document document = dom.createDocument("http://appgen.sourceforge.net/appgen-mapping", "appgen-mapping", null);
/* 129 */     Element root = document.getDocumentElement();
/* 130 */     root.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:schemaLocation", "http://appgen.sourceforge.net/appgen-mapping http://appgen.sourceforge.net/schema/appgen-mapping-1.0.0.xsd");
/*     */ 
/* 132 */     root.appendChild(createConnectionInformationElement(document));
/* 133 */     root.appendChild(createGenerationInformationElement(document));
/* 134 */     root.appendChild(createEntityListElement(document));
/*     */ 
/* 136 */     return document;
/*     */   }
/*     */ 
/*     */   private void createXmlFile(File file, Document document) throws IOException, TransformerException {
/* 140 */     Transformer transformer = newTransformer();
/*     */ 
/* 142 */     FileWriter writer = new FileWriter(file);
/*     */ 
/* 144 */     transformer.transform(new DOMSource(document), new StreamResult(writer));
/*     */   }
/*     */ 
/*     */   private String getXmlText(Document document) throws IOException, TransformerException {
/* 148 */     Transformer transformer = newTransformer();
/*     */ 
/* 150 */     ByteArrayOutputStream writer = new ByteArrayOutputStream();
/*     */ 
/* 152 */     transformer.transform(new DOMSource(document), new StreamResult(writer));
/*     */ 
/* 154 */     return writer.toString();
/*     */   }
/*     */ 
/*     */   private Transformer newTransformer() throws TransformerConfigurationException {
/* 158 */     TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* 159 */     transformerFactory.setAttribute("indent-number", new Integer(4));
/*     */ 
/* 161 */     Transformer transformer = transformerFactory.newTransformer();
/* 162 */     transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
/* 163 */     transformer.setOutputProperty("encoding", "UTF-8");
/* 164 */     transformer.setOutputProperty("method", "xml");
/* 165 */     transformer.setOutputProperty("indent", "yes");
/*     */ 
/* 167 */     return transformer;
/*     */   }
/*     */ 
/*     */   private Element createConnectionInformationElement(Document document) {
/* 171 */     ConnectionInformation connectionInformation = this.mappingData.getConnectionInformation();
/*     */ 
/* 173 */     Element element = document.createElement("connectionInformation");
/*     */ 
/* 175 */     Element databaseName = document.createElement("databaseName");
/* 176 */     databaseName.appendChild(document.createTextNode(connectionInformation.getName() == null ? "" : connectionInformation.getName()));
/* 177 */     element.appendChild(databaseName);
/*     */ 
/* 179 */     Element databaseSchema = document.createElement("databaseSchema");
/* 180 */     databaseSchema.appendChild(document.createTextNode(connectionInformation.getSchema() == null ? "" : connectionInformation.getSchema()));
/* 181 */     element.appendChild(databaseSchema);
/*     */ 
/* 183 */     return element;
/*     */   }
/*     */ 
/*     */   private Element createGenerationInformationElement(Document document) {
/* 187 */     GenerationInformation generationInformation = this.mappingData.getGenerationInformation();
/*     */ 
/* 189 */     Element element = document.createElement("generationInformation");
/*     */ 
/* 191 */     Element outputDir = document.createElement("outputDir");
/* 192 */     outputDir.appendChild(document.createTextNode(generationInformation.getOutputDir() == null ? "" : generationInformation.getOutputDir().getPath()));
/* 193 */     element.appendChild(outputDir);
/*     */ 
/* 195 */     Element packageName = document.createElement("packageName");
/* 196 */     packageName.appendChild(document.createTextNode(generationInformation.getPackageName() == null ? "" : generationInformation.getPackageName()));
/* 197 */     element.appendChild(packageName);
/*     */ 
/* 199 */     return element;
/*     */   }
/*     */ 
/*     */   private Element createEntityListElement(Document document) {
/* 203 */     List<Entity> entityList = this.mappingData.getEntityList();
/*     */ 
/* 205 */     Element element = document.createElement("entityList");
/*     */ 
/* 207 */     if (entityList != null) {
/* 208 */       for (Entity entity : entityList) {
/* 209 */         Element entityElement = document.createElement("entity");
/*     */ 
/* 211 */         entityElement.setAttribute("tableName", entity.getTableName() == null ? "" : entity.getTableName());
/* 212 */         entityElement.setAttribute("baseName", entity.getBaseName() == null ? "" : entity.getBaseName());
/* 213 */         entityElement.setAttribute("create", String.valueOf(entity.isCreate()));
/*     */ 
/* 215 */         entityElement.appendChild(createFieldListElement(document, entity.getFieldList()));
/*     */ 
/* 217 */         element.appendChild(entityElement);
/*     */       }
/*     */     }
/*     */ 
/* 221 */     if (element.getChildNodes().getLength() == 0) {
/* 222 */       element.appendChild(document.createTextNode(""));
/*     */     }
/*     */ 
/* 225 */     return element;
/*     */   }
/*     */ 
/*     */   private Element createFieldListElement(Document document, List<Field> fieldList) {
/* 229 */     Element element = document.createElement("fieldList");
/*     */ 
/* 231 */     for (Field field : fieldList) {
/* 232 */       Element fieldElement = document.createElement("field");
/*     */ 
/* 234 */       fieldElement.setAttribute("columnName", field.getColumnName() == null ? "" : field.getColumnName());
/* 235 */       fieldElement.setAttribute("columnType", field.getColumnType() == null ? "" : field.getColumnType());
/* 236 */       fieldElement.setAttribute("columnLength", String.valueOf(field.getColumnSize()));
/* 237 */       fieldElement.setAttribute("pkPosition", String.valueOf(field.getPkPosition()));
/* 238 */       fieldElement.setAttribute("lob", String.valueOf(field.isLob()));
/* 239 */       fieldElement.setAttribute("nullable", String.valueOf(field.isNullable()));
/* 240 */       fieldElement.setAttribute("fieldName", field.getFieldName() == null ? "" : field.getFieldName());
/* 241 */       fieldElement.setAttribute("fieldType", field.getFieldType() == null ? "" : field.getFieldType());
/* 242 */       fieldElement.setAttribute("create", String.valueOf(field.isCreate()));
/*     */ 
/* 244 */       element.appendChild(fieldElement);
/*     */     }
/*     */ 
/* 247 */     if (element.getChildNodes().getLength() == 0) {
/* 248 */       element.appendChild(document.createTextNode(""));
/*     */     }
/*     */ 
/* 251 */     return element;
/*     */   }
/*     */ 
/*     */   public void loadFromXml(File file) throws Exception {
/* 255 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 256 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 257 */     Document document = builder.parse(file);
/*     */ 
/* 259 */     loadMappingData(document);
/*     */   }
/*     */ 
/*     */   public void loadFromXml(String text) throws Exception {
/* 263 */     InputStream in = new ByteArrayInputStream(text.getBytes());
/*     */ 
/* 265 */     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
/* 266 */     DocumentBuilder builder = factory.newDocumentBuilder();
/* 267 */     Document document = builder.parse(in);
/*     */ 
/* 269 */     loadMappingData(document);
/*     */   }
/*     */ 
/*     */   private void loadMappingData(Document document) {
/* 273 */     Element root = document.getDocumentElement();
/* 274 */     Element connectionInformaitonElement = (Element)root.getElementsByTagName("connectionInformation").item(0);
/* 275 */     Element generationInformationElement = (Element)root.getElementsByTagName("generationInformation").item(0);
/* 276 */     Element entityListElement = (Element)root.getElementsByTagName("entityList").item(0);
/*     */ 
/* 278 */     ConnectionInformation connectionInformation = parseConnectionInformationElement(connectionInformaitonElement);
/* 279 */     GenerationInformation generationInformation = parseGenerationInformationElement(generationInformationElement);
/* 280 */     List entityList = parseEntityListElement(entityListElement);
/*     */ 
/* 282 */     this.mappingData = new MappingData(connectionInformation, generationInformation, entityList);
/*     */   }
/*     */ 
/*     */   private ConnectionInformation parseConnectionInformationElement(Element connectionInformaitonElement) {
/* 286 */     ConnectionInformation connectionInformation = new ConnectionInformation();
/*     */ 
/* 288 */     String databaseName = connectionInformaitonElement.getElementsByTagName("databaseName").item(0).getTextContent();
/* 289 */     String databaseSchema = connectionInformaitonElement.getElementsByTagName("databaseSchema").item(0).getTextContent();
/*     */ 
/* 291 */     connectionInformation.setName(databaseName);
/* 292 */     connectionInformation.setSchema(databaseSchema);
/*     */ 
/* 294 */     return connectionInformation;
/*     */   }
/*     */ 
/*     */   private GenerationInformation parseGenerationInformationElement(Element generationInformationElement) {
/* 298 */     GenerationInformation generationInformation = new GenerationInformation();
/*     */ 
/* 300 */     String outputDirPath = generationInformationElement.getElementsByTagName("outputDir").item(0).getTextContent();
/* 301 */     File outputDir = null;
/* 302 */     if ((outputDirPath != null) && (outputDirPath.length() > 0)) {
/* 303 */       outputDir = new File(outputDirPath);
/*     */     }
/* 305 */     String packageName = generationInformationElement.getElementsByTagName("packageName").item(0).getTextContent();
/*     */ 
/* 307 */     generationInformation.setOutputDir(outputDir);
/* 308 */     generationInformation.setPackageName(packageName);
/*     */ 
/* 310 */     return generationInformation;
/*     */   }
/*     */ 
/*     */   private List<Entity> parseEntityListElement(Element entityListElement) {
/* 314 */     List list = new ArrayList();
/*     */ 
/* 316 */     NodeList entityNodeList = entityListElement.getElementsByTagName("entity");
/*     */ 
/* 318 */     for (int i = 0; i < entityNodeList.getLength(); i++) {
/* 319 */       Element entityElement = (Element)entityNodeList.item(i);
/*     */ 
/* 321 */       Entity entity = new Entity();
/*     */ 
/* 323 */       String tableName = entityElement.getAttribute("tableName");
/* 324 */       String baseName = entityElement.getAttribute("baseName");
/* 325 */       boolean create = "true".equals(entityElement.getAttribute("create"));
/*     */ 
/* 327 */       entity.setTableName(tableName);
/* 328 */       entity.setBaseName(baseName);
/* 329 */       entity.setCreate(create);
/*     */ 
/* 331 */       Element fieldListElement = (Element)entityElement.getElementsByTagName("fieldList").item(0);
/* 332 */       List fieldList = parseFieldListElement(fieldListElement, entity);
/* 333 */       entity.setFieldList(fieldList);
/*     */ 
/* 335 */       list.add(entity);
/*     */     }
/*     */ 
/* 338 */     return list;
/*     */   }
/*     */ 
/*     */   private List<Field> parseFieldListElement(Element fieldListElement, Entity entity) {
/* 342 */     List list = new ArrayList();
/*     */ 
/* 344 */     NodeList fieldNodeList = fieldListElement.getElementsByTagName("field");
/*     */ 
/* 346 */     for (int i = 0; i < fieldNodeList.getLength(); i++) {
/* 347 */       Element fieldElement = (Element)fieldNodeList.item(i);
/*     */ 
/* 349 */       Field field = new Field(entity);
/*     */ 
/* 351 */       String columnName = fieldElement.getAttribute("columnName");
/* 352 */       String columnType = fieldElement.getAttribute("columnType");
/* 353 */       int columnLength = 0;
/*     */       try {
/* 355 */         columnLength = Integer.parseInt(fieldElement.getAttribute("columnLength"));
/*     */       } catch (Exception localException1) {
/*     */       }
/* 358 */       int pkPosition = 0;
/*     */       try {
/* 360 */         pkPosition = Integer.parseInt(fieldElement.getAttribute("pkPosition"));
/*     */       } catch (Exception localException2) {
/*     */       }
/* 363 */       boolean lob = "true".equals(fieldElement.getAttribute("lob"));
/* 364 */       boolean nullable = "true".equals(fieldElement.getAttribute("nullable"));
/* 365 */       String fieldName = fieldElement.getAttribute("fieldName");
/* 366 */       String fieldType = fieldElement.getAttribute("fieldType");
/* 367 */       boolean create = "true".equals(fieldElement.getAttribute("create"));
/*     */ 
/* 369 */       field.setColumnName(columnName);
/* 370 */       field.setColumnType(columnType);
/* 371 */       field.setColumnSize(columnLength);
/* 372 */       field.setPkPosition(pkPosition);
/* 373 */       field.setLob(lob);
/* 374 */       field.setNullable(nullable);
/* 375 */       field.setFieldName(fieldName);
/* 376 */       field.setFieldType(fieldType);
/* 377 */       field.setCreate(create);
/*     */ 
/* 379 */       list.add(field);
/*     */     }
/*     */ 
/* 382 */     return list;
/*     */   }
/*     */ 
/*     */   public MappingData getMappingData() {
/* 386 */     return this.mappingData;
/*     */   }
/*     */ 
/*     */   public void setMappingData(MappingData mappingData) {
/* 390 */     this.mappingData = mappingData;
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.xml.XmlData
 * JD-Core Version:    0.6.0
 */